/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.InqueryDAOInterface;
import com.wii.elpro.dao.hb.BusinessPartnerDAOImpl;
import com.wii.elpro.dao.hb.CabangDAOImpl;
import com.wii.elpro.dao.hb.FinishingDAOImpl;
import com.wii.elpro.dao.hb.InqueryDAOImpl;
import com.wii.elpro.dao.hb.ProductSpecificationImpl;
import com.wii.elpro.dao.hb.ProjectDAOImpl;
import com.wii.elpro.dao.hb.StandartSpecificationImpl;
import com.wii.elpro.dao.hb.UserDAOImpl;
import com.wii.elpro.entity.BusinessPartner;
import com.wii.elpro.entity.Cabang;
import com.wii.elpro.entity.Finishing;
import com.wii.elpro.entity.Inquery;
import com.wii.elpro.entity.ProductSpecification;
import com.wii.elpro.entity.Project;
import com.wii.elpro.entity.StandartSpecification;
import com.wii.elpro.entity.User;
import com.wii.elpro.web.form.InquiryUpdateForm;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author JollkY
 */
public class InquiryPreUpdateAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User users = (User)sess.getAttribute("userSession");       
        if(users==null){
            return mapping.findForward("gagalLogin");            
        }  
        String forward;
        long id = Long.parseLong(request.getParameter("id"));
        InqueryDAOInterface dao = new InqueryDAOImpl();
        InquiryUpdateForm f = (InquiryUpdateForm) form;
        ActionMessages messages = null;       
        try {
            Inquery inquiry = dao.getInquery(id);
            f.setId(inquiry.getId());
            f.setAlamat(inquiry.getAlamat());
            f.setCabang(inquiry.getCabang().getId());
            f.setFinishing(inquiry.getFinishing().getId());
            f.setKonsultan(inquiry.getKonsultan().getId());
            f.setKontraktor(inquiry.getKontraktor().getId());
            f.setNoQuotation(inquiry.getNoQuotation());
            f.setProductSpec(inquiry.getProductSpec().getId());
            f.setProject(inquiry.getProject().getId());
            f.setSales(inquiry.getSales().getId());
            f.setStandartSpec(inquiry.getStandartSpec().getId());
            f.setTanggal(inquiry.getTanggal());

            List<User> sales = new ArrayList<User>();
            // MARKETING ONLY
            for(User user : new UserDAOImpl().getUsers()) {
                if(user.getDepartemen().getKode().equalsIgnoreCase("MRKT")) {
                    sales.add(user);
                }
            }
            // ======= MARKETING ONLY ENDS HERE =======
            List<Project> projects = new ProjectDAOImpl().getProjects();
            List<ProductSpecification> productSpecs = new ProductSpecificationImpl().getProductSpecifications();
            List<StandartSpecification> standardSpecs = new StandartSpecificationImpl().getStandartSpecifications();
            List<Finishing> finishings = new FinishingDAOImpl().getFinishings();
            List<BusinessPartner> contractors = new BusinessPartnerDAOImpl().getBusinessPartnersByLevel(3);
            List<BusinessPartner> consultants = new BusinessPartnerDAOImpl().getBusinessPartnersByLevel(4);
            List<Cabang> cabangs = new CabangDAOImpl().getCabangs();

            // SET TO REQUEST
            request.setAttribute("sales", sales);
            request.setAttribute("projects", projects);
            request.setAttribute("productSpecs", productSpecs);
            request.setAttribute("standardSpecs", standardSpecs);
            request.setAttribute("finishings", finishings);
            request.setAttribute("contactors", contractors);
            request.setAttribute("consultants", consultants);
            request.setAttribute("cabangs", cabangs);
            // ====== ENDS HERE ======
            
            forward = "success";
        } catch (Exception ex) {
            messages = new ActionMessages();
            messages.add("message", new ActionMessage("customMessage", "Application is fail to retrieve Project Owner data. Please contact my developer."));
            saveMessages(sess, messages);
            ex.printStackTrace();
            forward = "fail";
        }
        return mapping.findForward(forward);
    }
}
